﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace cnFederal
{
    public class Nations
    {
        private static CnfiDataDataContext lnq;
        private static List<Nation> _list;

        public Nations() { }

        public List<Nation> List
        {
            get
            {
                return _list;
            }
            set
            {
                _list = value;
            }
        }

        public static void Load(string language, bool display = true)
        {
            if (string.IsNullOrWhiteSpace(language)) return;
            lnq = new CnfiDataDataContext();
            _list = new List<Nation>();
            if (_list != null)
            {
                List<Nation> liste = new List<Nation>();
                var b = from c in lnq.Nationalities
                        where c.Language.Trim().ToLower() == language.Trim().ToLower()
                            && c.Display == display
                        select c;

                foreach (var s in b)
                {
                    Nation nation = new Nation();
                    nation.Id = (int)s.Id;
                    nation.NationId = (int)s.NationId;
                    if (s.AuthorId != null)
                    nation.AuthorId = (int)s.AuthorId;
                    nation.Language = s.Language;
                    nation.ImageUrl = s.ImageUrl;
                    nation.Title = (string)s.Title;
                    nation.Header = (string)s.Header;
                    nation.MainContent = (string)s.MainContent;                    
                    if (s.DateRegistered != null)
                        nation.DateRegistered = (DateTime)s.DateRegistered;                   
                    if (s.Category != null)
                        nation.Category = (string)s.Category;
                    if (s.Display != null)
                        nation.Display = (bool)s.Display;
                    if (s.Viewed != null)
                        nation.Viewed = (int)s.Viewed;
                   
                    liste.Add(nation);

                }

                _list = liste;
            }
        }

        public void RefreshPostlist()
        {
            _list.Clear();
            _list = GetAllFaPosts();
        }

        public static List<Nation> GetAllFaPosts()
        {

            return Enumerable.Reverse(_list).ToList();
        }

        public static List<Nation> GetTopPosts(int number)
        {
            if (_list == null) return null;
            return _list.Take(number).ToList();
        }

        public static List<Nation> GetFaPostsByCategory(string category, bool display = true)
        {
            if (_list == null) return null;
            List<Nation> list = new List<Nation>();
            var b = from c in _list
                    where c.Category == category && c.Display == display
                    select c;
            foreach (var s in b)
            {
                Nation nation = new Nation();
                nation.Id = (int)s.Id;
                nation.NationId = (int)s.NationId;
                nation.Header = (string)s.Header;
                nation.MainContent = (string)s.MainContent;
                nation.Language = s.Language;
                nation.Title = (string)s.Title;
                nation.ImageUrl = (string)s.ImageUrl;
                if (s.AuthorId != null)
                    nation.AuthorId = (int)s.AuthorId;
                if (s.Category != null)
                    nation.Category = (string)s.Category;
                if (s.Display != null)
                    nation.Display = (bool)s.Display;
                if (s.Viewed != null)
                    nation.Viewed = (int)s.Viewed;
               
                nation.DateRegistered = (DateTime)s.DateRegistered;
               
                list.Add(nation);
            }

            return list;
        }


        public static List<Nation> GetFaPostsByNationAndLanguage(int nationId, string language, bool dislplay = true )
        {
            if (_list == null) return null;
            if(nationId < 0 || string.IsNullOrWhiteSpace(language))return null;
            List<Nation> list = new List<Nation>();
            var b = from c in _list
                    where c.NationId == nationId && c.Language.Trim().ToLower()
                    == language.Trim().ToLower() && c.Display == dislplay
                    select c;
            foreach (var s in b)
            {
                Nation nation = new Nation();
                nation.Id = (int)s.Id;
                nation.NationId = (int)s.NationId;
                nation.Header = (string)s.Header;
                nation.MainContent = (string)s.MainContent;
                nation.Title = (string)s.Title;
                nation.ImageUrl = (string)s.ImageUrl;
                nation.Language = (string)s.Language;
                if (s.AuthorId != null)
                    nation.AuthorId = (int)s.AuthorId;
                if (s.Category != null)
                    nation.Category = (string)s.Category;
                if (s.Display != null)
                    nation.Display = (bool)s.Display;
                if (s.Viewed != null)
                    nation.Viewed = (int)s.Viewed;

                nation.DateRegistered = (DateTime)s.DateRegistered;

                list.Add(nation);
            }

            return list;
        }


        

        public static Nation GetFaPostByCategory(string category, bool display= true)
        {
            if (_list == null) return null;
            Nation nation = null;
            var b = from c in _list
                    where c.Category == category && c.Display == display
                    select c;
            foreach (var s in b)
            {
                nation = new Nation();
                nation.Id = (int)s.Id;
                nation.NationId = (int)s.NationId;
                nation.Header = (string)s.Header;
                nation.MainContent = (string)s.MainContent;
                nation.Title = (string)s.Title;
                nation.ImageUrl = (string)s.ImageUrl;
                nation.Language = (string)s.Language;
                if (s.AuthorId != null)
                    nation.AuthorId = (int)s.AuthorId;
                if (s.Category != null)
                    nation.Category = (string)s.Category;
                if (s.Display != null)
                    nation.Display = (bool)s.Display;
                if (s.Viewed != null)
                    nation.Viewed = (int)s.Viewed;
                nation.DateRegistered = (DateTime)s.DateRegistered;
            }

            return nation;
        }

        public static Nation GetFaPostById(int id)
        {
            if (_list == null) return null;
            Nation nation = null;
            var b = from c in _list
                    where c.Id == id
                    select c;
            foreach (var s in b)
            {
                nation = new Nation();
                nation.Id = (int)s.Id;
                nation.NationId = (int)s.NationId;
                nation.Header = (string)s.Header;
                nation.MainContent = (string)s.MainContent;
                nation.Title = (string)s.Title;
                nation.Language = (string)s.Language;
                nation.ImageUrl = (string)s.ImageUrl;
                if (s.AuthorId != null)
                    nation.AuthorId = (int)s.AuthorId;
                if (s.Category != null)
                    nation.Category = (string)s.Category;
                if (s.Display != null)
                    nation.Display = (bool)s.Display;
                if (s.Viewed != null)
                    nation.Viewed = (int)s.Viewed;               
                nation.DateRegistered = (DateTime)s.DateRegistered;
            }

            return nation;
        }

        public static int CreateNew(Nation f)
        {
            int lastId = 0;
            try
            {
                Nationality fa = new Nationality();
                fa.NationId = f.NationId;
                fa.AuthorId = f.AuthorId;
                fa.DateRegistered = f.DateRegistered;
                fa.Header = f.Header;
                fa.ImageUrl = f.ImageUrl;
                fa.MainContent = f.MainContent;
                fa.Language = f.Language;
                fa.Title = f.Title;
                fa.Category = (string)f.Category;
                fa.Display = (bool)f.Display;
                fa.Viewed = (int)f.Viewed;
                lnq.Nationalities.InsertOnSubmit(fa);
                lnq.SubmitChanges();
                lnq.Connection.Close();

                lastId = (int)(from c in lnq.Links
                               select c.Id).Max();
            }
            catch (Exception ex)
            {
                
            }
            return lastId;
        }

        public static bool DeleteItem(int id)
        {
            bool deleted = false;
            var b = from f in lnq.Nationalities
                    where f.Id == id
                    select f;
            if (b != null && b.Count() > 0)
            {
                Nationality n = b.First();
                try
                {
                    lnq.Nationalities.DeleteOnSubmit(n);
                    lnq.SubmitChanges();
                    deleted = true;
                }
                catch (Exception ex)
                {
                    deleted = false;
                }
            }
            return deleted;
        }

        public static void Save(Nation fa)
        {
            var b = from f in lnq.Nationalities
                    where f.Id == fa.Id
                    select f;
            foreach (var n in b)
            {
                n.NationId = fa.NationId;
                n.AuthorId = fa.AuthorId;
                n.Header = fa.Header;
                n.Language = fa.Language;
                n.ImageUrl = fa.ImageUrl;
                n.MainContent = fa.MainContent;
                n.Title = fa.Title;
                n.Category = fa.Category;
                n.Display = fa.Display;
                n.Viewed = fa.Viewed;
                n.DateRegistered = fa.DateRegistered;
            }
            lnq.SubmitChanges();
        }




    }


    public class Nation
    {
        public int Id
        {
            get;
            set;
        }
        public int NationId
        {
            get;
            set;
        }
        public int AuthorId
        {
            get;
            set;
        }
        public string ImageUrl
        { get; set; }
        public string Title
        { get; set; }
        public string Header
        { get; set; }
        public string MainContent
        { get; set; }
        public string Category
        { get; set; }
        public bool Display
        { get; set; }
        public int Viewed
        { get; set; }
        public string Language
        { get; set; }
        public DateTime DateRegistered
        { get; set; }

    }

}